//
//  YWPosterPuzzleLayout1.m
//  YWPhotoEditMaster
//
//  Created by jun peng on 2023/9/4.
//

#import "YWPosterPuzzleLayout1.h"

@implementation YWPosterPuzzleLayout1

-(instancetype)init{
    if (self = [super init]) {
       
    }
    return self;
}
//当边界改变的时候，要不要重新布局，会多次调用prepareLayout，layoutAttributesForElementsInRect，layoutAttributesForItemAtIndexPath方法
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
    return YES;
}

//这个方法是collectionView一切都准备好了调用
-(void)prepareLayout{
    [super prepareLayout];
    
}


//在rect这个位置的排布和属性
-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
    
    NSMutableArray *array = [NSMutableArray array];
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    for (int i = 0; i<count; i++) {
        UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
        
        [array addObject:attrs];
        
    }
    
    return array;
    
}


//这个是把上面的代码搬过来，上面再调用这个方法
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
//  UICollectionViewLayoutAttributes *attrs =  [super layoutAttributesForItemAtIndexPath:indexPath];
    
    CGFloat collectionViewHeight = self.collectionView.height;
    CGFloat collectionViewWidth = self.collectionView.width;
    
    
    CGFloat leftInterval = 35;
    CGFloat rightInterval = 35;
    CGFloat mediumInterval = 10;
    CGFloat mediumIntervalForHeng = 30;
    CGFloat topInterval = 50;
    CGFloat bottomInterval = 50;
    CGFloat smallImageWidth = (collectionViewWidth - leftInterval - rightInterval - mediumInterval) * 1.0 / 2;
    CGFloat cellHeight = (collectionViewHeight - topInterval - bottomInterval - mediumIntervalForHeng) * 1.0 / 2;
    CGFloat bigImageWidth = cellHeight + 30;
    
    if (indexPath.item == 0) {
        attrs.frame = CGRectMake(leftInterval, topInterval, smallImageWidth, cellHeight);
    }
      else if (indexPath.item == 1) {
          
          attrs.frame = CGRectMake(leftInterval + mediumInterval + smallImageWidth, topInterval,smallImageWidth, cellHeight);
      }
      else if (indexPath.item == 2){
          attrs.frame = CGRectMake(leftInterval, topInterval + cellHeight + mediumIntervalForHeng, bigImageWidth, cellHeight);
      }
      else{
          //最多显示3个
          attrs.frame = CGRectMake(0, 0, 0, 0);
          attrs.hidden = YES;
      }

    return attrs;
}
@end
